package com.weishu.installment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishu.installment.domain.CwPaymentPlanEntity;
import com.weishu.installment.domain.SfContractEntity;
import com.weishu.installment.domain.TJrProduct;
import com.weishu.installment.domain.shoufang.*;
import com.weishu.installment.mapper.CwPaymentPlanEntityMapper;
import com.weishu.installment.mapper.TJrProductMapper;
import com.weishu.installment.service.SfContractEntityService;
import com.weishu.installment.mapper.SfContractEntityMapper;
import com.weishu.installment.utils.BeanUtils;
import com.weishu.installment.utils.MyJinRongUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author admin
* @description 针对表【t_sf_contract】的数据库操作Service实现
* @createDate 2023-05-08 09:55:59
*/
@Service
public class SfContractEntityServiceImpl extends ServiceImpl<SfContractEntityMapper, SfContractEntity>
    implements SfContractEntityService{

    @Autowired
    SfContractEntityMapper sfContractEntityMapper;
    @Autowired
    TJrProductMapper mapper;

    @Override
    public SfContractDetailsResponseDTO getSfContractStagingCalculation(Long sfContractId, Long productId) {
        System.out.println("getSfContractStagingCalculation查询收房贷计算详情处理Start...");
        System.out.println("----查询收房合同详情查询处理详情入参：{}"+JSON.toJSONString(sfContractId) + JSON.toJSONString(productId));
        SfContractEntity sfce = sfContractEntityMapper.selectById(sfContractId);
        SfContractDetailsResponseDTO sfdto = new SfContractDetailsResponseDTO();
        BeanUtils.copyProperties(sfce, sfdto);
        // 查询收房分期
        List<CwPaymentPlanEntity> payList = getCwPaymentPlanList(sfContractId);
        TJrProduct product = mapper.selectById(productId);
        // 计算授信额
        Double creditLine = MyJinRongUtils.div(product.getCreditLine(), 100, 2);
        double balance = 0;
        if (payList.size() > 0) {
            for (CwPaymentPlanEntity pay : payList) {
                balance = MyJinRongUtils.add(balance, pay.getPaymentAmount().doubleValue());
            }
        }
        // 获取期数
        int size = payList.size();
        Integer paymentType = sfce.getPaymentType();
        double period = MyJinRongUtils.mul(size, paymentType);
        // 期数
        sfdto.setPeriods(new Double(period).intValue());
        // 总金额
        sfdto.setBalance(balance);
        // 授信总额
        double sxBalance = MyJinRongUtils.mul(sfdto.getBalance(), creditLine);
        sfdto.setSxBalance(sxBalance);
        System.out.println("getSfContractStagingCalculation查询收房贷计算详情处理End...");
        return sfdto;
    }

    @Autowired
    CwPaymentPlanEntityMapper cwPaymentPlanEntityMapper;

    // 查询收房分期 - 付款计划（ 未付款）
    private List<CwPaymentPlanEntity> getCwPaymentPlanList(Long sfContractId) {
        Map<String, Object> param = new HashMap<>();
        param.put("contract_type", ContractTypeEnum.SF.getIndex());
        param.put("contract_id", sfContractId);
        param.put("pay_type", PaymentTypeEnum.SFFK.getIndex());
        param.put("pay_state", PayStatusEnum.WFK.getIndex());
        param.put("bill_type", PayPlanZdType.PTZD.getIndex());
        param.put("item_type", BusinessCostTypesEnum.ZUJIN.getIndex());

        return cwPaymentPlanEntityMapper.selectByMap(param);
    }

}




